from django.shortcuts import render
from  rest_framework.views import  APIView
from rest_framework.generics import ListAPIView,CreateAPIView
from rest_framework.response import Response

# Create your views here.
from django.core.files.storage import default_storage

from django.core.files.storage import default_storage
class Images(APIView):
    def post(self,request):
        file=request.data.get('file')
        types=['png','jpg']
        if file.name.split('.')[-1] not in types:
            return Response({'code':201,'msg':'文件类型错误'})
        if file.size>1024*1024*2:
            return Response({'code':201,'msg':'文件过大'})
        #通过default_storage 设置文件上传的文件路径 ， 文件名字，文件对象
        uploadfile=default_storage.save(file.name,file)
        url=default_storage.url(uploadfile)#捕获上传后的位置
        return Response({'url':url})

# /upload/1.jpg




from images import models,images_serializers


class Show(ListAPIView):
    queryset = models.Images.objects.all()
    serializer_class = images_serializers.Images_serializer


class Add(CreateAPIView):

    queryset = models.Images.objects.all()
    serializer_class = images_serializers.Images_serializer



class Adderss(APIView):
    def get(self,request):
        data=models.Address.objects.filter(number=None)
        ser=images_serializers.Adderss_serializer(data,many=True)

        info=ser.data
        return Response({"code":200,"msg":"ok","data":info})

